﻿using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using NetworkLibrary;
using System.Threading;
using System.Threading.Tasks;

namespace TestConsole
{
    class Program
    {
        private static readonly object _lock = new object();
        private static void print(ConsoleColor color, string msg, int top, int left)
        {
            lock (_lock)
            {
                Console.ForegroundColor = color;
                Console.CursorTop = top;
                Console.CursorLeft = left;
                Console.WriteLine(msg);
            }
        }
        private static async Task Task1(ConcurrentQueue<int> bag, IndexSet<string> list)
        {
            var random = new Random();
            while (true)
            {
                if (list.Count < 128)
                {
                    var item = Guid.NewGuid().ToString();
                    var index = list.Push(item);
                    bag.Enqueue(index);
                    print(ConsoleColor.Green, $"0", 1, index);
                }
                await Task.Delay(joinSpeed);
                if (joinSpeed < 50)
                    joinSpeed += 1;
            }
        }
        private static async Task Task2(ConcurrentQueue<int> bag, IndexSet<string> list)
        {
            var random = new Random();
            while (true)
            {
                await Task.Delay(popSpeed);
                if (bag.Count > 10 && bag.TryDequeue(out var index))
                {
                    var item = list.Pop(index);
                    print(ConsoleColor.Red, $"0", 1, index);
                }
                if (popSpeed > 10)
                    popSpeed -= 1;
            }
        }
        static int joinSpeed;
        static int popSpeed;
        static void Main(string[] args)
        {
            joinSpeed = 1;
            popSpeed = 200;
            var list = new ConcurrentQueue<int>();
            IndexSet<string> rlist = new IndexSet<string>();
            Task.WaitAll(Task1(list, rlist), Task2(list, rlist));

        }
    }
}
